// 习题 2-34 ⊕异或链表
// pre ⊕ p =p->next
// p ⊕ p->next =pre
#include <stdio.h>
#include <stdlib.h>

typedef struct xor_list
{
	int data;
	struct xor_list *next;
};

xor_list *xor_op(xor_list *left, xor_list *right)
{
	unsigned long x, y, z;
	x = (unsigned long)left;
	y = (unsigned long)right;
	z = x ^ y;
	return (xor_list *)z;
}
// 把 l[]转化成异或链表
// 需要两个指针 一个正向指针 一个逆向指针
xor_list *creat_list(int l[], int n)
{
	xor_list *left, *right;

	xor_list *list = (xor_list *)malloc(sizeof(xor_list));
	xor_list *pre = NULL;
	xor_list *p = list;
	for (int i = 0; i < n; i++)
	{
		xor_list *node = (xor_list *)malloc(sizeof(xor_list));
		node->data = l[i];
		if (i = 0)
		{
			left = xor_op(NULL, node);
			pre = NULL;
			p = node;
		}
		if (i = n - 1)
		{
			right = xor_op(node, NULL);
		}
		else
		{
			p->next = xor_op(pre, node);
		}
	}
	p->next = (xor_list *)((unsigned long)pre ^ NULL);
	return list;
}

void vist_list(xor_list *xl)
{
	xor_list *pre = NULL;
	xor_list *p = xl;
	while (p)
	{
		xor_list *now;
		now = (xor_list *)((unsigned long)pre ^ (unsigned long)p);
		printf("%d", now->data);
		pre = p;
		p = now;
	}
}
int main()
{
	/*******************code*******************/
	int l[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
	xor_list *xl = creat_list(l, 10);
	vist_list(xl);
	/******************************************/
	printf("\n\n****************************\n");
	printf("Press Enter key to continue\n");
	getchar();
	return 0;
	/******************************************/
}
